//
//  YSNewFeaturesCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/19.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  新特性页

#import "YSNewFeaturesCtrl.h"
#import "CustomTabBarCtrl.h"

@interface YSNewFeaturesCtrl ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) NSMutableArray *imgArray;
@property (assign, nonatomic) BOOL isOut;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) UIButton *goBtn;
@end

@implementation YSNewFeaturesCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self makeNewFeatureView];
}

-(BOOL)shouldHideStatusBar{
    return YES;
}

-(void)makeNewFeatureView{
    _imgArray = [NSMutableArray array];
   
    for (int i = 1; i < 6; i++) {
        if(SCREENH==480){// 4s 4
            [_imgArray addObject:[NSString stringWithFormat:@"bg_guide_4_%d",i]];
        }else if(SCREENH==568){// 5s 5
            [_imgArray addObject:[NSString stringWithFormat:@"bg_guide_5_%d",i]];
        }else if(SCREENH==667){// 6
            [_imgArray addObject:[NSString stringWithFormat:@"bg_guide_6_%d",i]];
        }else if(SCREENH==736){// 6 plus
            [_imgArray addObject:[NSString stringWithFormat:@"bg_guide_6p_%d",i]];
        }
    }
    
    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    scrollView.contentSize=CGSizeMake(SCREENW*_imgArray.count, SCREENH);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.bounces = NO;
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, SCREENH-50, SCREENW, 10)];
     pageControl.userInteractionEnabled = NO;
     pageControl.numberOfPages = _imgArray.count;//总的图片页数
     pageControl.currentPage = 0; //当前页
     pageControl.currentPageIndicatorTintColor = ThemeColor;
     pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    [pageControl updateCurrentPageDisplay];
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
     _pageControl = pageControl;
    
    for (int i=0; i<_imgArray.count; i++) {
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(i*SCREENW, 0, SCREENW, SCREENH)];
        img.image= [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",_imgArray[i]]];
        img.userInteractionEnabled = YES;
        [scrollView addSubview:img];
    }
    
    UIButton *goBtn = [[UIButton alloc] init];
    UIImage *image = [UIImage imageNamed:@"go"];
    [goBtn setImage:image forState:UIControlStateNormal];
    goBtn.frame = CGRectMake(0, 0, SCREENW, image.size.height);
    goBtn.centerX = scrollView.centerX + (_imgArray.count - 1)*SCREENW;
    goBtn.y = scrollView.height - 110;
    [goBtn addTarget:self action:@selector(toMain) forControlEvents:UIControlEventTouchUpInside];
    _goBtn = goBtn;
    [scrollView addSubview:goBtn];
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toMain)];
//    [[self.scrollView.subviews lastObject] addGestureRecognizer:tap];
    
    //遮盖切图上面的圆点
//    UIView *cover = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(pageControl.frame), SCREENW, SCREENH-CGRectGetMaxY(pageControl.frame))];
//    [cover setBackgroundColor:[UIColor whiteColor]];
//    [self.view addSubview:cover];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.frame;
    int currentPage = offset.x / bounds.size.width;
    [_pageControl setCurrentPage:currentPage];
    
    if (currentPage == _imgArray.count -1) {
        _pageControl.hidden = YES;
        _goBtn.hidden = NO;
    }else{
        _goBtn.hidden = YES;
        _pageControl.hidden = NO;
    }
}

-(void)changePage:(UIPageControl*)pageControl{
    NSInteger currentPage = self.pageControl.currentPage;
    [self.scrollView setContentOffset:CGPointMake(SCREENW * currentPage, 0) animated:YES];
}

-(void)toMain{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [DefaultsHelper setObject:[SystemUtil getAppVersion] forKey:kAppVersion];
    UIWindow *window = theApp.window;
    window.rootViewController = [[CustomTabBarCtrl alloc]init];
    [window makeKeyAndVisible];
}

@end
